package com.nth.android.ar;

import android.location.Location;

public class AROverlayItem implements Comparable<AROverlayItem> {

	private String title;
	private String description;
	private Location location;
	private float distance;
	private float rotation;

	private boolean isVisible;
	private Vector3 cMarker = new Vector3();
	private Vector3 signMarker = new Vector3();
	private Vector3 locationVector = new Vector3();

	public AROverlayItem(String title, Location location) {
		this(title, "", location);
	}

	public AROverlayItem(String title, String description, Location location) {
		super();
		this.title = title;
		this.description = description;
		this.location = location;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public float getDistance() {
		return distance;
	}

	public float getRotation() {
		return rotation;
	}

	public Vector3 getLocationVector() {
		return locationVector;
	}

	public boolean isVisible() {
		return isVisible;
	}

	public Vector3 getcMarker() {
		return cMarker;
	}

	public double getLatitude() {
		return location.getLatitude();
	}

	public double getLongitude() {
		return location.getLongitude();
	}

	public double getAltitude() {
		return location.getAltitude();
	}

	public void locationUpdated(Location newLocation) {
		float[] dist = new float[3];
		Location.distanceBetween(getLatitude(), getLongitude(), newLocation.getLatitude(), newLocation.getLongitude(), dist);
		distance = dist[0];

		if (location.getAltitude() == 0.0) location.setAltitude(newLocation.getAltitude());
		convLocToVec(newLocation, location, locationVector);
	}

	public void calc(ARCamera viewCam, float addX, float addY) {
		calcCmarker(ARUtil.origin, viewCam, addX, addY);
		calcVisible(viewCam);
		rotation = ARUtil.getAngle(cMarker.x, cMarker.y, signMarker.x, signMarker.y) + 90;
	}

	private void calcCmarker(Vector3 originalPoint, ARCamera viewCam, float addX, float addY) {
		Vector3 tmpa = new Vector3(originalPoint);
		Vector3 tmpc = new Vector3(ARUtil.upVector);
		tmpa.add(locationVector);
		tmpc.add(locationVector);
		tmpa.sub(viewCam.lco);
		tmpc.sub(viewCam.lco);
		tmpa.prod(viewCam.transform);
		tmpc.prod(viewCam.transform);

		Vector3 tmpb = new Vector3();
		viewCam.projectPoint(tmpa, tmpb, addX, addY);
		cMarker.set(tmpb);
		viewCam.projectPoint(tmpc, tmpb, addX, addY);
		signMarker.set(tmpb);
	}

	private void calcVisible(ARCamera viewCam) {
		isVisible = false;
		if (cMarker.z < -1f) {
			isVisible = true;
		}
	}

	private static void convLocToVec(Location org, Location gp, Vector3 v) {
		float[] z = new float[1];
		z[0] = 0;
		Location.distanceBetween(org.getLatitude(), org.getLongitude(), gp.getLatitude(), org.getLongitude(), z);
		float[] x = new float[1];
		Location.distanceBetween(org.getLatitude(), org.getLongitude(), org.getLatitude(), gp.getLongitude(), x);
		double y = gp.getAltitude() - org.getAltitude();
		if (org.getLatitude() < gp.getLatitude()) z[0] *= -1;
		if (org.getLongitude() > gp.getLongitude()) x[0] *= -1;

		v.set(x[0], (float) y, z[0]);
	}

	@Override
	public int compareTo(AROverlayItem another) {
		return Double.compare(getDistance(), another.getDistance());
	}

}
